﻿///////////////////////////////////////////////////////////////////////////////
// File:        Main.cc
// Author:      671643387@qq.com
// Date:        2016年1月14日 下午2:12:19
// Description:
///////////////////////////////////////////////////////////////////////////////

#include <csignal>
#include <iostream>
#if defined(__WINDOWS__)
#include <crtdbg.h>
#endif

#include <yxs/net/SocketManager.h>
#include "EchoServerSocketFactory.h"
#include "EchoServerSocket.h"
#include "EchoServerStatics.h"

bool _grun = true;
__YXS_SSM_ *_gssm = NULL;

EchoServerStatics _gstatics;

void __signal_handler(int signal)
{
    if (signal == SIGINT)
    {
        _grun = false;
        _gssm->Close();
    }
}

int main(int argc, char *argv[])
{
#if defined(__WINDOWS__)
    WSADATA wsa;
    WSAStartup(MAKEWORD(2,2), &wsa);
#endif

    {
        ::signal(SIGINT, __signal_handler);

        ::std::string addr = "0.0.0.0";
        int port = 8887;
        int thread = 1;

        if (argc >= 4)
        {
            addr = argv[1];
            port = ::atoi(argv[2]);
            thread = ::atoi(argv[3]);
        }

        __YXS_SSM_ smgr(__YXS_SSF_PTR_(new EchoServerSocketFactory()));
        if (smgr.Open(addr, port, 1024, thread))
        {
            _gssm = &smgr;

            std::cout << "echo server is listen on " << addr << ":" << port << std::endl;

            while (_grun)
            {
                __YXS_THREAD_::Sleep(1000);
                _gstatics.DumpAndReset();
            }

            smgr.Join();
        }

    }

#if defined(__WINDOWS__)
    WSACleanup();
    _CrtDumpMemoryLeaks();
#endif
    return 0;
}